# -*- coding: utf-8 -*-
# by Twiguard

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from builtins import *
from xml.dom.minidom import parse
import sys, os

app = QApplication(sys.argv)

class Main(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.file = os.getcwd() + os.sep + "db.xml"
        dom1 = parse(self.file)
        n_main = dom1.firstChild
        n_head = dom1.getElementsByTagName('head')[0]
        n_body = dom1.getElementsByTagName('body')[0]

        # Main Window Settings
        self.setWindowTitle('Some Database Manager - %s' % self.file)
        self.resize(640, 480)
        self.setWindowIcon(QIcon('Search.ico'))

        self.sb = self.statusBar()
        self.sb.showMessage(self.file)

        # Actions
        quit = QAction(QIcon('exit.svg'), 'Exit', self)
        quit.setShortcut('Esc')
        self.connect(quit, SIGNAL('triggered()'), SLOT('close()'))

        open = QAction(QIcon('deb.svg'), 'Open', self)
        open.setShortcut('ctrl+o')
        self.connect(open, SIGNAL('triggered()'), self.open)

        save = QAction(QIcon('floppy.svg'), 'Save', self)
        save.setShortcut('ctrl+s')
        self.connect(save, SIGNAL('triggered()'), SLOT('close()'))

        add = QAction(QIcon('add.svg'), 'Add', self)
        add.setShortcut('Ins')
        self.connect(add, SIGNAL('triggered()'), SLOT('close()'))

        edit = QAction(QIcon('edit.svg'), 'Edit', self)
        edit.setShortcut('Enter')
        self.connect(edit, SIGNAL('triggered()'), SLOT('close()'))

        delete = QAction(QIcon('remove.svg'), 'Delete', self)
        delete.setShortcut('Del')
        self.connect(delete, SIGNAL('triggered()'), SLOT('close()'))

        help = QAction(QIcon('help.svg'), 'Help', self)
        help.setShortcut('F1')
        self.connect(help, SIGNAL('triggered()'), self.help)

        about = QAction(QIcon('info.svg'), 'About', self)
        about.setShortcut('F2')
        self.connect(about, SIGNAL('triggered()'), self.about)

        # Menu
        m = self.menuBar()
        m_file = m.addMenu('File')
        m_file.addAction(open)
        m_file.addAction(save)
        m_file.addAction(quit)

        m_edit = m.addMenu('Edit')
        m_edit.addAction(add)
        m_edit.addAction(edit)
        m_edit.addAction(delete)

        m_help = m.addMenu('Help')
        m_help.addAction(help)
        m_help.addAction(about)

        # Toolbar
        tb = self.addToolBar('just toolbar')
        tb.addAction(open)
        tb.addAction(save)
        tb.addAction(add)
        tb.addAction(edit)
        tb.addAction(delete)
        tb.addAction(quit)

        # Tabs & Tables
        cw = QTabWidget()
        self.setCentralWidget(cw)

        tab1 = QTableWidget()
        cw.addTab(tab1, "users")
        tab1.setColumnCount(4)
        h_1 = ['ID', 'first name', 'surname', 'birth-year']
        tab1.setHorizontalHeaderLabels(h_1)

        tab2 = QTableWidget()
        cw.addTab(tab2, "laptops")
        tab2.setColumnCount(4)
        h_2 = ['brand', 'type', 'obtained (year)', 'price']
        tab2.setHorizontalHeaderLabels(h_2)

        tab3 = QTableWidget()
        cw.addTab(tab3, "allocation")
        tab3.setColumnCount(3)
        h_3 = ['user', 'laptop', 'obtained (year)']
        tab3.setHorizontalHeaderLabels(h_3)

    # Functions
    def add(self):
        return 0

    def edit(self):
        return 0

    def delete(self):
        return 0

    def open(self):
        filename = QFileDialog.getOpenFileName(w_main, "Open file ...", os.getcwd(), "*.xml")
        self.file = open(filename)
        data = self.file.read()
        self.sb.showMessage(self.file.name, 10000)
        self.setWindowTitle('Some Database Manager - %s' % self.file.name)

    def save(self):
        return 0

    def help(self):
        QMessageBox.information (self, "Help", "No help here, sorry. =(", QMessageBox.Ok)

    def about(self):
        QMessageBox.information (self, "About", '<qt>Some Database Manager<br /><br />Support:<br /><a href = "mailto://twiguard@gmail.com">twiguard at gmail</a><br />or<br /><a href = "http://www.twitter.com/Twiguard_CZ">Twitter</a></qt>', QMessageBox.Ok)

    class AddDialog(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)

            self.setGeometry(300, 300, 350, 80)
            self.setWindowTitle('AddDialog')
            self.setStyleSheet("QWidget { background-color: grey }")

            self.button = QPushButton('Dialog', self)

            self.button.move(20, 20)
            self.connect(self.button, SIGNAL('clicked()'), SLOT('close()'))
            self.setFocus()

            self.label = QLineEdit(self)
            self.label.move(130, 22)


        def showDialog(self):
            text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

            if ok:
                self.label.setText(str(text))

# The End
w_main = Main()
w_main.show()
QObject.connect(w_main, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
sys.exit(app.exec_())
